/**
 * 用户服务
 */
angular
.module('myApp')
.factory('userManager', function($http, $q) {
  return {
    /**
     * 用户登录
     */
    login: function(mobile, password) {
    
      var def = $q.defer();

      $http.post('/auth/login', {
        mobile: mobile,
        password: password
      })
      .then(function(response) {
        (response.data.code === 200)
          ? def.resolve(response.data.data)
          : def.reject(response.data.data);
      }, function(response) {
        def.reject({msg: response.status+': '+response.statusText});
      });

      return def.promise;
    },
    /**
     * 重置密码
     */
    resetPassword: function(mobile, password, repassword, captcha) {
      var def = $q.defer();

      $http.post('/auth/reset_password', {
        mobile: mobile,
        new_password: password,
        password: repassword,
        code: captcha
      })
      .then(function(response) {
        (response.data.code === 200)
          ? def.resolve(response.data.data)
          : def.reject(response.data.data);
      }, function(response) {
        def.reject({msg: response.status+': '+response.statusText});
      });

      return def.promise;
    },
    /**
     * 发送验证码
     */
    sendCaptcha: function(mobile) {
      var def = $q.defer();

      var query = {
        mobile: mobile
      }

      $http.get('/code/send', {params: query})
        .then(function(response) {
          (response.data.code === 200)
            ? def.resolve(response.data.data)
            : def.reject(response.data.data);
        }, function(response) {
          def.reject({msg: response.status+': '+response.statusText});
        });

      return def.promise;
    }
  }
});
